package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*; //for convinience
/**
 * 
 * User interface - main menu
 * The entry-point for the application:
 * Create an instance of class MainMenuUI and the constructor starts the application
 * 
 * @author (kbh)
 * @version (2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 *                    
 * (KBH): 2008.10.24: Change af manimenuStart to if
 * (Anders M, Jesper, Claus (Gruppe 1, Tema Design)): 2012.11.16: added functionallity 
 */
public class MainMenuUI
{
  //the reference to the ControlLayer
   private AddressBookUI adrUI;//references to other UI objects
   private DvdUI dvdUI;
   private LoanCtr loanCtr;
   
   /**
    * Contructor
    */
   public MainMenuUI()
   {
       loanCtr = new LoanCtr();
   }
   
   /**
    * main menu loop
    */
   public void mainMenuStart()
   {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            if(choise == 1)
            {
                startAddressBook();
            }
            else{
                    if(choise == 2){ 
                        startDvd();
                    }
                    else{
                        if(choise == 3){
                        //start loanController
                        startLoan();
                            }//end if
                        else{
                            writeEnd();
                            exit = true;
                        }//end else
                    }// end else
             }//end else
        }//end while
    }//end start
    
    /**
     * Writes main menu to screen and asks for a choise
     */
   private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** MainMenu ***");
            System.out.println(" (1) AddressBook");
            System.out.println(" (2) DVDs");
            System.out.println(" (3) Loan");
            System.out.println(" (4) Close");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            
            return choise;
    }
    
    /**
     * Starts address book menu
     */
    private void startAddressBook()
    {
        adrUI= new AddressBookUI();
        adrUI.start();
        //the adrUI needs to have a reference to the address controller
        //main controller can return references to the other controllers
     
    }
    
    /**
     * Starts DVD menu
     */
    private void startDvd()
    {
        dvdUI= new DvdUI();
    }
    
    /**
     * Writes closing message to screen
     */
    private void writeEnd()
    {
        System.out.println(" The System is shuting down ");
    }
    
    /**
     * Starts loan menu
     */
    private void startLoan()
    {
        writeLoanMenu();
        
    }
    
    /**
     * Writes loan menu to screen and asks for a choise
     */
    private void writeLoanMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            System.out.println("\f### Loan Menu ###");
            System.out.println("# 1) new loan");
            System.out.println("# 2) update loan");
            System.out.println("# 3) Show all Loans");
            System.out.println("# 4) back");
            System.out.println("Make your choise: ");
            int choise = getUserInt();
            
            if(choise == 1)
            {
                newLoan();
            }
            else
            {
                if(choise == 2)
                {
                    updateLoan();
                }
                else
                {
                    if(choise == 3)
                    {
                        showLoans();
                    }
                    else
                    {
                        exit = true;
                    }
                }
            }
            
            getUserString(); //<--pause
        }
    }
    
    /**
     * Creates a new loan by asking for a person (id), an example (serial number),
     * the current date and the loaning-period.
     */
    private void newLoan()
    {
        Person person = adrUI.getPerson();
        Example example = dvdUI.getExample();
        String date = inputDate();
        String period = inputPeriod();
        
        loanCtr.newLoan(person, example, date, period);
    }
    
    /**
     * Updates a loan object (i.e. asks if it is still lend out).
     * It will ask for a person (by ID) and an example (by serial number)
     */
    private void updateLoan()
    {
        System.out.println("Is it still lend out? ");
        System.out.println(" 1) yes");
        System.out.println(" 2) no");
        
        int choise = getUserInt();
        
        boolean isLendOut = true;
        if(choise == 2)
        {
            isLendOut = false;
        }
        else if(choise > 2)
        {
            System.out.println("Wrong number!");
            updateLoan();  
        }
        
        Person person = adrUI.getPerson();
        Example example = dvdUI.getExample();
        String date = inputDate();
        
        loanCtr.updateLoan(person, example, date, isLendOut);
    }
    
    /**
     * Prints information about all loans to the screen,
     * or "There are no loans!" if no loans exists
     */
    private void showLoans()
    {
        if(loanCtr.getLoans().size() == 0)
        {
            System.out.println("There are no loans!");
        }
        for(Loan loan : loanCtr.getLoans())
        {
            System.out.println(loan);   //<-- prints toString on Loan.
        }
        
    }
    
    /**
     * Asks the user to write the current date
     */
    private String inputDate()
    {
        System.out.println("Write the current date: ");
        return getUserString();
        
    }
    
    /**
     * Asks the user to write the loan-period
     */
    private String inputPeriod()
    {
        System.out.println("Write the period in which the loan is meant to be: ");
        return getUserString();
    }
    
    /**
     * Returns a string written by the user
     */
    private String getUserString()
    {
        Scanner keyboard = new Scanner(System.in);
        return keyboard.nextLine();
    }
    
    /**
     * Returns an int written by the user
     */
    private int getUserInt()
    {
        Scanner keyboard = new Scanner(System.in);
        return keyboard.nextInt();
    }
 }
